"""
@Author : 合肥一元数智教育科技有限公司
@Date :  2025/6/11 14:10
@Description : 
 pymysql 实现数据的增删改操作
"""
import pymysql

"""
步骤:
1. 获取连接
2. 获取游标对象     操作sql语句的对象
3. 处理结果集
4. 释放资源 
mysql 增删改操作  事务    提交 commit  回滚 rollback 
"""
mysql_connection = pymysql.connect(host='localhost', port=3306, user='root', passwd='root', db='python2512')
cursor = mysql_connection.cursor()
# 添加用户信息到user表
# insert into user(username,password)values(user1,111111)
# str_sql = 'INSERT INTO `python2512`.`user`(`username`, `password`) VALUES ("user1", "123456")'
# str_sql = 'update user set username = "user2",password="222222" where id = 7'
str_sql = 'delete from user where id = 7'
row = cursor.execute(str_sql)
print(row)

# 手动提交事务  commit  增删改生效
mysql_connection.commit()
# 资源释放
cursor.close()
mysql_connection.close()
